package com.tarena.lbs.user.service.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.common.encoder.JwtEncoder;
import com.tarena.lbs.common.enums.Roles;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.dao.repository.UserRepository;
import com.tarena.lbs.user.pojo.param.AdminLoginParam;
import com.tarena.lbs.user.pojo.param.UserLoginParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.vo.LoginVO;
import com.tarena.lbs.user.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class LoginServiceImpl implements LoginService {

    @Autowired
    private AdminRepository adminRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private JwtEncoder jwtEncoder;

    @Override
    public LoginVO doLogin(AdminLoginParam param) throws BusinessException {
        String phone = param.getPhone();
        AdminPO po = adminRepository.getAdminByPhone(phone);//数据层命名 最好通过名称知道业务功能
        Asserts.isTrue(po==null,new BusinessException("-2","该手机号不存在"));
        matchPassword(param.getPassword(),po.getAccountPassword());
        String jwt=generateJwt(po);
        return new LoginVO(jwt,po.getId(),po.getNickname());
    }

    @Override
    public LoginVO userLogin(UserLoginParam param) throws BusinessException {
        //1 使用phone查询存在 抛异常
        String phone = param.getPhone();
        UserPO po = userRepository.getByPhone(phone);
        Asserts.isTrue(po==null,new BusinessException("-2","该手机号不存在小程序用户"));
        //2 密码对比 复用之前代码
        matchPassword(param.getPassword(),po.getPassword());
        //3 封装UserPO生成创建的jwt
        String jwt=generateJwt(po);
        LoginVO loginVO = new LoginVO(jwt, po.getId(), po.getNickName());
        return loginVO;

    }

    private String generateJwt(AdminPO po) throws BusinessException {

        UserPrinciple userPrinciple=new UserPrinciple();
        userPrinciple.setId(po.getId());
        userPrinciple.setNickname(po.getNickname());
        //角色 在后台管理中 有2个角色 admin表格 type=0 平台 type=1 商家
        //在认证对象放的角色是一个枚举Role ADMIN SHOP USER
        userPrinciple.setRole(po.getAccountType()==0? Roles.ADMIN:Roles.SHOP);
        try {
            return jwtEncoder.generateToken(userPrinciple);
        } catch (BusinessException e) {
            throw new RuntimeException(e);
        }
    }

    private void matchPassword(String password, String accountPassword) throws BusinessException {
        boolean equals = StringUtils.equals(password, accountPassword);
        Asserts.isTrue(!equals,new BusinessException("-2","密码不相等"));
    }
    private String generateJwt(UserPO po) throws BusinessException {
        UserPrinciple userPrinciple=new UserPrinciple();
        userPrinciple.setId(po.getId());
        userPrinciple.setNickname(po.getNickName());
        //当前user角色没得选 只有Roles.USER
        userPrinciple.setRole(Roles.USER);
        return jwtEncoder.generateToken(userPrinciple);
    }
}